import { defineConfig, ConfigEnv, loadEnv, UserConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
//element-plus按需导入
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: env.VITE_APP_CONTEXT_PATH,
    resolve: {
      /*src文件夹别名配置*/
      alias: {
        '@': path.resolve(__dirname, 'src')
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    // https://cn.vitejs.dev/config/#resolve-extensions
    plugins: [
      vue(),
      //element-plus按需导入
      AutoImport({
        //自动导入vue相关函数
        imports: ['vue'],
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        //配置自定义主题色
        resolvers: [ElementPlusResolver({ importStyle: 'sass' })]
      }),
      //svg图标
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]'
      })
    ],
    server: {
      host: '0.0.0.0',
      port: Number(env.VITE_APP_PORT),
      open: true,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: 'http://localhost:8081',
          // target: 'http://lylportal:8081',
          changeOrigin: true,
          ws: true,
          rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          // 2. 自动导入定制化样式文件进行样式覆盖
          additionalData: `
          @use "@/style/element/index.scss" as *;
          @use "@/style/var.scss" as *;
        `
        }
      }
    }
  };
});
